/*****************************************************************************/
/* TVCTape - Videoton TV Computer Tape Emulator                              */
/* Direct Digital Synthesis (wave generation)                                */
/*                                                                           */
/* Copyright (C) 2013 Laszlo Arvai                                           */
/* All rights reserved.                                                      */
/*                                                                           */
/* This software may be modified and distributed under the terms             */
/* of the BSD license.  See the LICENSE file for details.                    */
/*****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Includes
#include "DDS.h"
#include "WaveMapper.h"
#include "Main.h"

///////////////////////////////////////////////////////////////////////////////
// Module global variables
static DWORD l_dds_accumulator = 0;
static DWORD l_dds_increment = 0;

///////////////////////////////////////////////////////////////////////////////
// Initialize DDS
void InitDDS(void)
{
	l_dds_accumulator = 0;
}

///////////////////////////////////////////////////////////////////////////////
// Sine table
static BYTE l_sine_table[DDS_TABLE_LENGTH] = 
{
	0x80, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x92, 0x95,
	0x97, 0x9A, 0x9D, 0xA0, 0xA3, 0xA6, 0xA8, 0xAB,
	0xAE, 0xB1, 0xB3, 0xB6, 0xB9, 0xBB, 0xBE, 0xC0,
	0xC3, 0xC5, 0xC7, 0xCA, 0xCC, 0xCE, 0xD1, 0xD3,
	0xD5, 0xD7, 0xD9, 0xDB, 0xDD, 0xDF, 0xE0, 0xE2,
	0xE4, 0xE5, 0xE7, 0xE8, 0xEA, 0xEB, 0xEC, 0xEE,
	0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF4, 0xF5,
	0xF6, 0xF6, 0xF7, 0xF7, 0xF7, 0xF8, 0xF8, 0xF8,
	0xF8, 0xF8, 0xF8, 0xF8, 0xF7, 0xF7, 0xF7, 0xF6,
	0xF6, 0xF5, 0xF4, 0xF4, 0xF3, 0xF2, 0xF1, 0xF0,
	0xEF, 0xEE, 0xEC, 0xEB, 0xEA, 0xE8, 0xE7, 0xE5,
	0xE4, 0xE2, 0xE0, 0xDF, 0xDD, 0xDB, 0xD9, 0xD7,
	0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xCA, 0xC7, 0xC5,
	0xC3, 0xC0, 0xBE, 0xBB, 0xB9, 0xB6, 0xB3, 0xB1,
	0xAE, 0xAB, 0xA8, 0xA6, 0xA3, 0xA0, 0x9D, 0x9A,
	0x97, 0x95, 0x92, 0x8F, 0x8C, 0x89, 0x86, 0x83,
	0x80, 0x7D, 0x7A, 0x77, 0x74, 0x71, 0x6E, 0x6B,
	0x69, 0x66, 0x63, 0x60, 0x5D, 0x5A, 0x58, 0x55,
	0x52, 0x4F, 0x4D, 0x4A, 0x47, 0x45, 0x42, 0x40,
	0x3D, 0x3B, 0x39, 0x36, 0x34, 0x32, 0x2F, 0x2D,
	0x2B, 0x29, 0x27, 0x25, 0x23, 0x21, 0x20, 0x1E, 
	0x1C, 0x1B, 0x19, 0x18, 0x16, 0x15, 0x14, 0x12,
	0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0C, 0x0B,
	0x0A, 0x0A, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
	0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x0A,
	0x0A, 0x0B, 0x0C, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
	0x11, 0x12, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1B,
	0x1C, 0x1E, 0x20, 0x21, 0x23, 0x25, 0x27, 0x29,
	0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x36, 0x39, 0x3B,
	0x3D, 0x40, 0x42, 0x45, 0x47, 0x4A, 0x4D, 0x4F,
	0x52, 0x55, 0x58, 0x5A, 0x5D, 0x60, 0x63, 0x66,
	0x69, 0x6B, 0x6E, 0x71, 0x74, 0x77, 0x7A, 0x7D
};

///////////////////////////////////////////////////////////////////////////////
// Generate signal
bool GenerateDDSSignal(DWORD in_frequency, DWORD in_cycle_count)
{
	BYTE sample;
	DWORD prev_accumulator;
	DWORD dds_increment;
	bool success = true;
	
	dds_increment = (DWORD)(((__int64)in_frequency * MAX_DWORD) / SAMPLE_RATE);

	while(in_cycle_count > 0 && success)
	{
		sample = l_sine_table[(BYTE)(l_dds_accumulator >> 24)];

		prev_accumulator = l_dds_accumulator;
		l_dds_accumulator += dds_increment;

		if(l_dds_accumulator < prev_accumulator)
			in_cycle_count--;

		success = WMWriteSample(sample);
	}

	return success;
}

///////////////////////////////////////////////////////////////////////////////
// Generate silence
bool GenerateDDSSilence(WORD in_length_in_ms)
{
	DWORD sample_count = (DWORD)in_length_in_ms * SAMPLE_RATE / 1000;
	bool success = true;
	
	while(sample_count > 0 && success)
	{
		WMWriteSample(BYTE_SAMPLE_ZERO_VALUE);
		sample_count--;
	}

	l_dds_accumulator = 0;

	return success;
}
